//
//  PaperMoreViewManager.m
//  ZYOnlineSchool
//
//  Created by 毕武侠 on 2017/2/28.
//  Copyright © 2017年 bwx. All rights reserved.
//

#import "PaperMoreViewManager.h"
#import "PaperMoreView.h"
#import "PaperModuleModel.h"
#import "PaperFavoriteModel.h"
#import "PaperTestModel.h"

@interface PaperMoreViewManager ()

@property (nonatomic, strong) PaperMoreView *view;

@property (nonatomic, strong, readwrite) RACSignal *wrongSignal;    // 纠错Signal
@property (nonatomic, strong, readwrite) RACSignal *favoriteSignal; // 收藏Signal
@property (nonatomic, strong, readwrite) RACSignal *shareSignal;    // 分享Signal

@property (nonatomic, strong) PaperAnswerModel *model;

@end

@implementation PaperMoreViewManager
@dynamic view;
@dynamic model;

- (void)setupConfigure {
    self.view = [[PaperMoreView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    if (self.superView) {
        [self.superView addSubview:self.view];
    }
    self.view.hidden = YES;
    
    self.wrongSignal    = [self.view getSignalWithDictionary:@{@"action" : @"wrong"}];
    self.favoriteSignal = [[self.view getSignalWithDictionary:@{@"action" : @"favorite"}] map:^id(id value) {
        return self.model;
    }];
    self.shareSignal    = [[self.view getSignalWithDictionary:@{@"action" : @"share"}] map:^id(id value) {
        return self.model;
    }];
}

- (void)setMoreSignal:(RACSignal *)moreSignal {
    _moreSignal = moreSignal;
    
    @weakify(self)
    [moreSignal subscribeNext:^(id x) {
        @strongify(self)
        if ([x isKindOfClass:[PaperAnswerModel class]] || [x isKindOfClass:[PaperTestModel class]] ) {
            self.model = x;
            [self.view showWithFavorite:self.model.is_favorites withAnimation:YES];
        }
    }];
}

- (PaperAnswerModel *)getAnswerModelAtPoint:(NSInteger)value {
    __block PaperAnswerModel *answerModel = nil;
    
    __block NSInteger index = -1;
    [self.datas enumerateObjectsUsingBlock:^(PaperModuleModel *moduleModel, NSUInteger idx, BOOL * _Nonnull stop) {
        if (moduleModel.showModule == YES) {
            index++;
        }
        if (index == value) {
            *stop = YES;
        }
        
        [moduleModel.module_question enumerateObjectsUsingBlock:^(PaperAnswerModel * model, NSUInteger idx, BOOL * _Nonnull stop) {
            index++;
            if (value == index) {
                answerModel = model;
                *stop = YES;
            }
        }];
    }];
    return answerModel;
}

// 收藏接受
- (void)setFavoriteReceiveSignal:(RACSignal *)favoriteReceiveSignal {
    _favoriteReceiveSignal = favoriteReceiveSignal;
    [_favoriteReceiveSignal subscribeNext:^(id x) {
        PaperFavoriteModel *favoriteModel = x;
        [self.view showWithFavorite:favoriteModel.is_favorites withAnimation:YES];
    }];
}

@end
